package com.group56.model.dao.Impl;


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.group56.model.dao.CoachDAO;
import com.group56.model.entity.Coach;
import com.group56.model.tools.jsonFileProcessor;
import com.group56.utils.DBSrcMenu;

import java.util.ArrayList;

public class CoachDAOImpl implements CoachDAO {

    public static void main(String[] args) {

            jsonFileProcessor file = new jsonFileProcessor(DBSrcMenu.COACH_JSON.getSrc());
            new CoachDAOImpl().update(new Coach("","2","2","2","2","2","2"),"2");
            ArrayList<Coach> records = new CoachDAOImpl().getAll(file);
            System.out.println(records);
    }

    public ArrayList<Coach> getAll(jsonFileProcessor file){
        StringBuffer buffer = file.read();
        if (!buffer.toString().equals(""))
            return new Gson().fromJson(buffer.toString(), new TypeToken<ArrayList<Coach>>() {}.getType());
        return null;
    }

    public boolean insert(Coach coach) {
        jsonFileProcessor file = new jsonFileProcessor(DBSrcMenu.COACH_JSON.getSrc());
        ArrayList<Coach> records = getAll(file);
        if (records!=null && selectByID(coach.getCoachID())==null) {
            if (records.size()!=0)
                coach.setCoachID(String.valueOf(Integer.parseInt(records.get(records.size() - 1).getCoachID())+1));
            else
                coach.setCoachID("1");
            records.add(coach);
            return file.rewrite(records);
        }
        return false;
    }

    public boolean delete(String coachID) {
        jsonFileProcessor file = new jsonFileProcessor(DBSrcMenu.COACH_JSON.getSrc());
        ArrayList<Coach> records = getAll(file);
        if (records!=null) {
            for (Coach c : records)
                if (c.getCoachID().equals(coachID)){
                    records.remove(c);
                    return file.rewrite(records);
                }
        }
        return false;
    }

    public boolean update(Coach coach, String coachID) {
        jsonFileProcessor file = new jsonFileProcessor(DBSrcMenu.COACH_JSON.getSrc());
        ArrayList<Coach> records = getAll(file);
        if (records!=null) {
            for (int i=0;i<records.size();i++){
                if (records.get(i).getCoachID().equals(coachID)) {
                    records.set(i, coach);
                    return file.rewrite(records);
                }
            }
        }
        return false;
    }

    public Coach selectByID(String coachID) {
        ArrayList<Coach> records = getAll(new jsonFileProcessor(DBSrcMenu.COACH_JSON.getSrc()));
        if (records!=null) {
            for (Coach c : records)
                if (c.getCoachID().equals(coachID))
                    return c;
        }
        return null;
    }
    public Coach selectByName(String name){
        ArrayList<Coach> records = getAll(new jsonFileProcessor(DBSrcMenu.COACH_JSON.getSrc()));
        if (records!=null) {
            for (Coach c : records)
                if (c.getName().equals(name))
                    return c;
        }
        return null;
    }
}
